//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.data.biz.utils.pageLimit;
import com.data.biz.utils.CoreConstants;
import com.data.biz.utils.CoreEntity;
import com.data.biz.utils.servlet.ServletHolderFilter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class PageLimit extends CoreEntity {
    protected Log logger = LogFactory.getLog(this.getClass());
    public static final String PAGE_COUNT = "limit.count";
    public static final String PAGE_SIZE = "limit.length";
    public static final String PAGE_START = "limit.start";
    public static final String PAGE_ENABLE = "limit.enable";
    private boolean limited;
    private boolean isLimited;
    private Integer currentPageNo;
    private Integer pageLength;
    private Integer totalCount;
    private Integer startRowNo;
    private Integer endRowNo;
    private boolean onlyGetTotalCnt;

    public PageLimit() {
    }

    public PageLimit(boolean limited, Integer pageStart, Integer pageSize, Integer totalCount) {
        this.limited = limited;
        if (limited) {
            this.isLimited = false;
            this.currentPageNo = pageStart;
            this.pageLength = pageSize;
            this.totalCount = totalCount;
            if (this.currentPageNo > 1) {
                this.startRowNo = (this.currentPageNo - 1) * this.pageLength + 1;
            } else {
                this.startRowNo = this.currentPageNo;
            }

            this.endRowNo = this.startRowNo + this.pageLength - 1;
        }

    }

    public PageLimit(HttpServletRequest request) {
        Map<String, Object> paramMap = ServletHolderFilter.getContext().getParamMap();
        this.limited = paramMap != null && paramMap.get("limit.enable") != null && new Boolean(String.valueOf(paramMap.get("limit.enable")));
        if (this.limited) {
            this.isLimited = false;
            if (paramMap != null) {
                this.currentPageNo = Integer.valueOf(String.valueOf(paramMap.get("limit.start")));
                this.pageLength = Integer.valueOf(String.valueOf(ObjectUtils.defaultIfNull(paramMap.get("limit.length"), CoreConstants.SUPPORT_LIMIT_PAGESIZE)));
                this.totalCount = Integer.valueOf(String.valueOf(ObjectUtils.defaultIfNull(paramMap.get("limit.count"), "0")));
            } else {
                this.currentPageNo = Integer.valueOf(request.getParameter("limit.start"));
                this.pageLength = Integer.valueOf(StringUtils.defaultIfEmpty(request.getParameter("limit.length"), CoreConstants.SUPPORT_LIMIT_PAGESIZE));
                this.totalCount = Integer.valueOf(StringUtils.defaultIfEmpty(request.getParameter("limit.count"), "0"));
            }

            if (this.currentPageNo > 1) {
                this.startRowNo = (this.currentPageNo - 1) * this.pageLength + 1;
            } else {
                this.startRowNo = this.currentPageNo;
            }

            this.endRowNo = this.startRowNo + this.pageLength - 1;
        }

        paramMap.remove("limit.enable");
        paramMap.remove("limit.count");
        paramMap.remove("limit.length");
        paramMap.remove("limit.start");
    }

    public PageLimit(Integer currentPageNo, Integer pageLength, Integer totalCount) {
        this.limited = true;
        this.isLimited = false;
        this.currentPageNo = currentPageNo == null ? 1 : currentPageNo;
        this.pageLength = pageLength == null ? Integer.valueOf(CoreConstants.SUPPORT_LIMIT_PAGESIZE) : pageLength;
        this.totalCount = totalCount == null ? 0 : totalCount;
        if (currentPageNo > 1) {
            this.startRowNo = (currentPageNo - 1) * pageLength + 1;
        } else {
            this.startRowNo = currentPageNo;
        }

        this.endRowNo = this.startRowNo + pageLength - 1;
    }

    public Integer getStartRowNo() {
        return this.startRowNo;
    }

    public Integer getEndRowNo() {
        return this.endRowNo;
    }

    @ModelAttribute("totalPageCount")
    public Integer getTotalPageCount() {
        Integer totalPageCount = null;
        if (this.limited) {
            if (this.totalCount % this.pageLength == 0) {
                totalPageCount = this.totalCount / this.pageLength;
            } else {
                totalPageCount = this.totalCount / this.pageLength + 1;
            }
        }

        return totalPageCount;
    }

    @ModelAttribute("hasPreviousPage")
    public boolean getHasPreviousPage() {
        return this.currentPageNo > 1;
    }

    @ModelAttribute("hasPreviousPage")
    public boolean getHasNextPage() {
        return this.currentPageNo < this.getTotalPageCount();
    }

    public boolean limited() {
        return this.limited;
    }

    public boolean isLimited() {
        return this.isLimited;
    }

    public void setLimited(boolean isLimited) {
        this.isLimited = isLimited;
    }

    public Integer getTotalCount() {
        return this.totalCount;
    }

    public Integer getCurrentPageNo() {
        return this.currentPageNo;
    }

    public Integer getPageLength() {
        return this.pageLength;
    }

    public void setTotalCount(Integer totalCount) {
        this.isLimited = true;
        this.totalCount = totalCount;
    }

    public boolean isOnlyGetTotalCnt() {
        return this.onlyGetTotalCnt;
    }

    public void setOnlyGetTotalCnt(boolean onlyGetTotalCnt) {
        this.onlyGetTotalCnt = onlyGetTotalCnt;
    }
}
